var mongodb = require('mongodb').MongoClient;
var settings = require('../settings');
// var crypto = require('crypto');
// var ObjectID = require('mongodb').ObjectID;
// var async = require('async');
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/runoob';




function Copy(copy) {
    this.author = copy.author;
    this.title = copy.title;
    this.text = copy.text;
}

module.exports = Copy;

//储存用户信息
Copy.prototype.save = function (callback) {

    var copy = {
        author: this.author,
        title: this.title,
        text:this.text
    };

    //打开数据库
    mongodb.connect(settings.url, function (err, db) {

        if (err) {
            return callback(err); //错误返回err信息
        }
        //读取users集合
        db.collection('copys', function (err, collection) {
            if (err) {
                db.close();
                return callback(err);	//如果错误返回err
            }
            //将用户的数据插入Users集合
            collection.insert(copy, {
                safe: true
            }, function (err, copy) {
                db.close();
                if (err) {
                    return callback(err); //如果错误返回err
                }
                callback(null, copy); //成功，err为null,并返回存储后的用户文档
            });
        });
    });
};


